package cn.lili.controller;

import cn.hutool.core.util.StrUtil;
import cn.lili.common.aop.annotation.PreventDuplicateSubmissions;
import cn.lili.common.utils.ResultUtil;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.share.service.ShareService;
import cn.lili.util.ToolsUtil;
import com.pojos.UserShareBindRegister;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;

@RestController
@AllArgsConstructor
@Api(tags = "分享控制器")
@RequestMapping("/share")
public class ShareController {
    private final ShareService shareService;


    /**
     * 获取分享二维码
     */
    @GetMapping("/getShareQrCode")
    @ApiOperation("获取分享二维码")
    public ResultMessage<Object> getShareQrCode() {
        return shareService.getShareQrCode();
    }

    @PostMapping("/apiUserLogin")
    @ApiOperation("分享绑定操作记录")
    @PreventDuplicateSubmissions
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value = "手机号", required = true, paramType = "query"),
            @ApiImplicitParam(name = "scene", value = "场景", required = true, paramType = "header"),
            @ApiImplicitParam(name = "uuid", value = "uuid", required = true, paramType = "header"),
            @ApiImplicitParam(name = "sessionId", value = "会话id", required = true, paramType = "query"),
            @ApiImplicitParam(name = "code", value = "短信验证码", required = true, paramType = "query"),
            @ApiImplicitParam(name = "superior", value = "上级account", required = true, paramType = "query")
    })
    public ResultMessage<Object> apiUserLogin(
            @NotNull(message = "mobile cannot be empty") @RequestParam String mobile,
            @NotNull(message = "scene cannot be empty") @RequestHeader String scene,
            @NotNull(message = "code cannot be empty") @RequestParam String sessionId,
            @NotNull(message = "code cannot be empty") @RequestParam String code,
            @NotNull(message = "code cannot be empty") @RequestParam String superior,
            @RequestHeader String uuid
    ) {
        return ResultUtil.data(shareService.apiUserLogin(sessionId, superior));
    }


    @PostMapping("/shareRegisterAndBindParent")
    @ApiOperation("分享注册并绑定上级")
    @PreventDuplicateSubmissions
    public ResultMessage<Object> shareRegisterAndBindParent(@RequestBody UserShareBindRegister userShare,
                                                            @RequestHeader String deviceid,
                                                            HttpServletRequest request) {
        if (StringUtils.isEmpty(userShare.getMobile())) {
            return ResultUtil.error(400, "手机号码不能为空");
        }
        if(StrUtil.isEmpty(deviceid)){
            return ResultUtil.error(400, "clientId不可为空");
        }
        userShare.setClientId(deviceid);
        userShare.setIp(ToolsUtil.getIpAddr(request));
        return shareService.shareRegisterAndBindParent(userShare);
    }


    @PostMapping("/shareRegisterSetPassword")
    @ApiOperation("分享注册设置密码")
    @PreventDuplicateSubmissions
    public ResultMessage<Object> shareRegisterSetPassword(@RequestBody UserShareBindRegister userShare,
                                                          @RequestHeader String sessionId,
                                                          @RequestHeader String token) {
        if (StringUtils.isEmpty(userShare.getMobile())) {
            return ResultUtil.error(400, "手机号码不能为空");
        }
        userShare.setToken(token);
        return shareService.shareRegisterSetPassword(userShare, sessionId);
    }

}
